<template>
  <el-dialog
    title="选择 支付类型"
    :visible.sync="dialogVisible"
    width="300px">
    <div>
      <div>订单金额: <span class="red b" v-text="'￥'+ToMoney(order.price)"></span></div>
      <div v-if="payType===0">当前账户余额: <span class="red b" v-text="'￥'+ToMoney(currMoney)"></span></div>
      <div class="pay-choose-box">
        <div class="title">支付方式</div>
        <div class="item" @click="payType=0">
          <div class="left">
            <div class="icon money-pay"></div>
            <span>余额支付</span>
          </div>
          <div class="right">
            <div class="pay-choose-icon" :class="payType===0?'on':''"></div>
          </div>
        </div>
        <div class="item" @click="payType=1">
          <div class="left">
            <div class="icon wechat-pay"></div>
            <span>微信</span>
          </div>
          <div class="right">
            <div class="pay-choose-icon" :class="payType===1?'on':''"></div>
          </div>
        </div>
        <div class="item" @click="payType=2">
          <div class="left">
            <div class="icon ali-pay"></div>
            <span>支付宝</span>
          </div>
          <div class="right">
            <div class="pay-choose-icon" :class="payType===2?'on':''"></div>
          </div>
        </div>
        <div class="item" @click="payType=3">
          <div class="left">
            <div class="icon enterprise-pay"></div>
            <span>对公支付</span>
          </div>
          <div class="right">
            <div class="pay-choose-icon" :class="payType===3?'on':''"></div>
          </div>
        </div>
      </div>

    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirm">确认支付</el-button>
    </div>
  </el-dialog>

</template>

<script>
export default {
  name: 'ChooseOrderPayTypeDialog',
  data: function () {
    return {
      callback:null,
      dialogVisible:false,
      payType:0,
      currMoney:0,
      order:{
        id:0,
        orderNo:'',
        price:0,
      }
    }
  },
  mounted () {
    let $this = this
    $this.bus.$on('ChooseOrderPayTypeDialogCallBus', function (order,callback) {
      $this.dialogVisible = true
      $this.order=order
      $this.callback=callback
      $this.getCurrMoney()
      $this.$forceUpdate()
    })

  },
  methods: {
    confirm:function () {
      let $this = this
      if ($this.payType===2){
        return $this.successTip(dict.OrderPayArr[$this.payType]+' 暂未开通')
      }
      if (this.payType===0){
        if ($this.currMoney<$this.order.price){
          return $this.successTip('当前账户余额不足','warning')
        }
      }
      // 对公支付
      if ($this.payType===3){
        $this.$router.push({
          name: 'EnterprisePayInfo',
          query: {
            type: 'order',
            orderNo: $this.order.orderNo,
            orderData: encodeURI(JSON.stringify({
              productList: $this.order.productData,
              price: $this.order.price,
            }))
          }
        })
        $this.dialogVisible = false
        return
      }
      $this.MyRequest($this.ApiDoNameShop + 'Order/pay', {
        id:$this.order.id,
        payType:$this.payType,
      }, function (data) {
        // 1.账户余额支付,支付成功
        if ($this.payType===0){
          $this.$router.push({
            name: 'PaySuccess',
            query: {
              type: 'order',
              payType: 0,
              orderNo: $this.order.orderNo,
              orderData: encodeURI(JSON.stringify({
                productList: $this.order.productData,
                price: $this.order.price,
              }))
            }
          })

          if (typeof $this.callback==='function'){
            $this.callback()
          }
          $this.dialogVisible = false
          return;
        }

        if ($this.payType===1){
          // 微信支付
          var wxPayPlus = api.require('wxPayPlus')
          wxPayPlus.payOrder({
            apiKey: data.appid,
            orderId: data.prepayid,
            mchId: data.partnerid,
            nonceStr: data.noncestr,
            timeStamp: data.timestamp,
            package: data.package,
            sign: data.sign
          }, function (ret, err) {
            if (ret.status) {
              $this.$message.success('支付成功')
              $this.$router.push({
                name: 'PaySuccess',
                query: {
                  type: 'order',
                  payType: 0,
                  orderNo: $this.order.orderNo,
                  orderData: encodeURI(JSON.stringify({
                    productList: $this.order.productData,
                    price: $this.order.price,
                  }))
                }
              })
            } else {
              api.alert({
                title: '支付结果',
                msg:"支付失败: "+JSON.stringify(err)
              })
            }
          })

          if (typeof $this.callback==='function'){
            $this.callback()
          }
        }

        if ($this.payType===2){
          // 支付宝支付

        }


        $this.$forceUpdate()
      }, function (msg) {
        $this.$message.error(msg)
      })

      console.log('this.payType',this.payType)
    },
    // 获取当前账户余额
    getCurrMoney:function () {
      let $this = this
      $this.MyRequest($this.ApiDoNameShop + 'Member/info', {}, function (data) {
        $this.currMoney = data.money
        $this.$forceUpdate()
      }, function (msg) {
        $this.$message.error(msg)
      })
    }
  }
}
</script>

<style scoped>

</style>
